﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using AMM.Model;
using AMM.Model.Project;

namespace AMM.DataAccess.Project {
    public class MaterialRepository : BaseRepository<Material> {

        private int _pageSize;

        public MaterialRepository(int pageSize) {
            this._pageSize = pageSize;
        }

        public Material FetchMaterialById(int basicsId) {
            using (var ctx = this.Context) {
                return ctx.Materials.Find(basicsId);
            }
        }

        public List<Material> FetchMaterialList() {
            using (var ctx = this.Context) {
                return ctx.Materials.Include("Basic").ToList();
            }
        }

        public void Update(Material material, Material oldMaterial) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        if (oldMaterial.AttachmentId.HasValue) {
                            Attachment oldAttachment = ctx.Attachments.Find(oldMaterial.AttachmentId);
                            oldAttachment.IsLinked = false;
                            ctx.Attachments.Attach(oldAttachment);
                            ctx.Entry(oldAttachment).State = EntityState.Modified;
                        }
                        if (material.AttachmentId.HasValue) {
                            Attachment newAttachment = ctx.Attachments.Find(material.AttachmentId);
                            newAttachment.IsLinked = true;
                            ctx.Attachments.Attach(newAttachment);
                            ctx.Entry(newAttachment).State = EntityState.Modified;
                        }
                        ctx.Materials.Attach(material);
                        ctx.Entry(material).State = EntityState.Modified;
                        ctx.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }

        public List<Material> FetchMaterialList(MaterialSearchModel searchModel, int pageIndex) {
            using (var ctx = this.Context) {
                var query = ctx.Materials
                    .Include("Basic")
                    .Include("Basic1")
                    .Where(x => !string.IsNullOrEmpty(x.Code));

                if (!string.IsNullOrEmpty(searchModel.Code)) {
                    query = query.Where(x => x.Code.Contains(searchModel.Code));
                }
                if (!string.IsNullOrEmpty(searchModel.Name)) {
                    query = query.Where(x => x.Name.Contains(searchModel.Name));
                }
                if (searchModel.MaterialTypeId.HasValue) {
                    query = query.Where(x => x.MaterialTypeId == searchModel.MaterialTypeId.Value);
                }
                if (searchModel.IsEnabled.HasValue) {
                    query = query.Where(x => x.IsEnabled == searchModel.IsEnabled.Value);
                }

                searchModel.Records = query.Count();
                pageIndex = (pageIndex - 1) * this._pageSize > searchModel.Records ? 1 : pageIndex;

                return query.OrderBy(x => x.Code).Skip((pageIndex - 1) * this._pageSize).Take(this._pageSize).ToList();
            }
        }

        public void DeleteMultipleMaterial(params int[] materialIds) {
            using (var ctx = this.Context) {
                using (DbContextTransaction trans = ctx.Database.BeginTransaction()) {
                    try {
                        ctx.Materials.Where(x => materialIds.Contains(x.MaterialId)).ToList().ForEach(x => {
                            ctx.Materials.Remove(x);
                        });
                        ctx.SaveChanges();
                        trans.Commit();
                    }
                    catch (Exception ex) {
                        trans.Rollback();
                        throw ex;
                    }
                }
            }
        }
    }
}
